<template>
  <div class="form-layout">
    <!--左侧导航-->
    <div class="nav-left">
      <customAnchor :itemList="[{ name: 'box-base', title: '基础信息' }]" />
    </div>
    <!--表单区域-->
    <div class="form-right" ref="formRight">
      <customForm :data="form" :rules="propRules" ref="editForm" :disabled="propReadonly">
        <template #content>
          <div id="box-base" class="box-title">基础信息</div>
          <customFormItem label="设备名称" name="name" :parent="_self" :form="form" />
          <customFormItem label="设备编码" name="code" :parent="_self" :form="form" help="自动生成" disabled />
          <customFormItem
            label="所属试剂组"
            name="reagentTeamId"
            :parent="_self"
            :form="form"
            :type="constCommon.formItemType.selectStatic.name"
            :config="{ key: 'id', label: 'name', value: 'id', options: propDictionaries.reagentTeamId }"
          />
          <customFormItem
            label="检验设备"
            name="flagTesting"
            :parent="_self"
            :form="form"
            :type="constCommon.formItemType.switch.name"
          />
        </template>
      </customForm>
    </div>
  </div>
</template>

<script>
import customAnchor from '@/components/custom/form/customAnchor.vue'

export default {
  data() {
    return {
      form: {},
      viewData: {},
      originName: '',
    }
  },
  methods: {
    print(data) {
      let rows = []
      rows.push({
        matrName: data.name,
        hrpCode: data.code,
        sn: data.sn,
      })

      let printerLabelServerid = this.utils.loadUserState(this, 'printer_label_serverid')
      let printerLabelServerTypeKey = this.utils.loadUserState(this, 'printer_label_server_type_key')

      if (!printerLabelServerid) {
        this.utilMessage.warning('请先到工作台设置标签打印服务器')
        return
      }

      if (printerLabelServerTypeKey == 'rfid') {
        //RFID打印机
        this.utilVue.loadShow(true)
        let params = {
          snArr: [],
          printerId: printerLabelServerid,
        }
        rows.forEach((obj) => {
          params.snArr.push(obj.sn)
        })
        this.$ajax.post('/tool/printRFID', params, { timeout: 1000 * 60 * 10 }).then((resp) => {
          if (resp.code == 0) {
            this.utilMessage.success(resp.msg)
          }
          this.utilVue.loadHide()
          this.$refs.dataview.ocRefresh()
        })
      } else if (printerLabelServerTypeKey == 'label') {
        //普通标签打印机
        let param = {
          rows,
          hideColumns: ['lot', 'mfgName', 'expDate', 'matrSpec', 'packageUnit'],
        }
        global.utilPrint.openLabel(param)
      }
    },
  },
  created() {
    const that = this
    this.$emit('event-register', {
      validate: (callback) => {
        return this.$refs.editForm.validate(callback)
      },
      beforeSave: () => {
        this.$emit('data-callback', this.form)
        return true
      },
      afterMove: (formData, initData) => {
        this.form = global.utils.jsonClone(formData)
        this.originName = formData.name
        // 新增，设置默认值
        if (!this.form.id) {
          this.$set(this.form, 'flagTesting', true)
        }
      },
      afterSave: () => {},
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {},
      beforeFlow: (flowButtonKey) => {
        return true
      },
      afterFlow: (flowButtonKey) => {},
      beforeClose: () => {
        return true
      },
      async afterClick(button, data, viewForm) {
        const { key: buttonKey } = button
        if (buttonKey == 'printsn') {
          if (!viewForm.form.flagTesting) {
            viewForm.utilMessage.warning('此设备不是试剂检验设备，不支持打印')
            return
          }
          viewForm.print(data)
        }
      },
    })
  },
  props: {
    propParent: Object,
    propInitData: Object,
    propPrimaryKey: String,
    propDictionaries: Object,
    propCurrentRow: Object,
    propViewFormData: Object,
    propReadonly: Boolean,
    propStatus: String,
    propRules: Object,
  },
  components: {
    customAnchor,
  },
  computed: {},
}
</script>
